package cn.bdqn.crm.service;

import cn.bdqn.crm.domain.dao.SysUserMapper;
import cn.bdqn.crm.domain.pojo.SysUser;
import cn.bdqn.crm.domain.vo.SysUserVo;
import cn.bdqn.crm.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class SysUserServiceImpl implements SysUserService{

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public SysUserVo findByLogin(String userName, String userPwd) {
        SysUserVo userVo = new SysUserVo();
        Map<String,Object> map = new HashMap<>();
        map.put("userName",userName);
        map.put("userPassword",userPwd);
        try {
            List<SysUser> userList =  sysUserMapper.getSysUserListByMap(map);
            if(userList!=null&&userList.size()>0){
                SysUser user = userList.get(0);
                userVo.setRetCode("200");
                userVo.setId(user.getId());
                userVo.setUserName(user.getUserName());
                userVo.setUserPassword(user.getUserPassword());
                String tokenId = "TOKEN-USER-"+ UUID.randomUUID().toString().replace("-","").toUpperCase();
                System.out.println(tokenId);
                userVo.setToken(tokenId);
                redisUtil.set(tokenId,userVo,1800);
            }else{
                userVo.setRetCode("300");
            }
        } catch (Exception e) {
            userVo.setRetCode("500");
            e.printStackTrace();
        }
        return userVo;
    }
}
